<?php
namespace Agent\Controller;
use Think\Controller;
class OrderController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('agent');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
		//数据库连接基础配置-begin
		C('db_config.DB_TYPE',C('DB_TYPE'));
		C('db_config.DB_HOST',C('DB_HOST'));
		C('db_config.DB_USER',C('DB_USER'));
		C('db_config.DB_PWD',C('DB_PWD'));
		C('db_config.DB_PORT',C('DB_PORT'));
		C('db_config.DB_CHARSET',C('DB_CHARSET'));
		C('db_config.DB_NAME',C('erp_dbname'));
		//数据库连接基础配置-end
    }
	public function order_owner_pushorder(){
		$m=M('Checkbill',C('DB_PREFIX_T'), C('db_config'));
		$p=M('Pushorder',C('DB_PREFIX_T'), C('db_config'));
		$param=I('param.','','trim');
		$pay_no=$param['pay_no'];
		$map['pay_no']=$pay_no;
		$m_info=$m->where($map)->find();
		$ids=$m_info['ids'];
		if(empty($m_info)){
			alert_json(0,"没有找到对应单号");
		}else{
			if($m_info['pay_status']==1){
				alert_json(0,"已经支付过了");
			}
			$Api=A('Api');
			$res_sss=$Api->owner_pushorder($pay_no,"$ids");//同步订单到货主erp
			file_put_contents('./log/order_owner_pushorder.log',date('Y-m-d H:i:s').$data.PHP_EOL,FILE_APPEND);//日志导出sql
			$map_p['id']=array('in',"$ids");
			$data_status['pay_status']=1;
			$res_m=$m->where($map)->save($data_status);
			$res_p=$p->where($map_p)->save($data_status);
			alert_json(1,"支付成功");
		}
	}
	public function make_alipay_no(){
		$param=I('param.','','trim');
		$ids=$param['ids'];
		$pay_no=$param['order_no'];
		$fee=$param['total_fee'];
		$m=M('Checkbill', C('DB_PREFIX_T'), C('db_config'));
		$p=M('Pushorder', C('DB_PREFIX_T'), C('db_config'));
		$map['id']=array('in',"$ids");
		$p_list=$p->where($map)->select();
		foreach($p_list as $ks=>$vs){
			if($vs['pay_status']==1){
				alert_json(0,"订单编号".$vs['order_no']."已支付");
			}
			if($vs['pay_status']==2){
				alert_json(0,"订单编号".$vs['order_no']."等待支付");
			}
		}
		$data['pay_no']=$pay_no;
		$data['fee']=$fee;
		$data['ids']=$ids;
		$data['pay_status']=2;
		$data['add_time']=time();
		$data['agent_id']=session('agent.id');
		$data['status']=1;
		$res=$m->add($data);
		// echo $m->getlastsql();
		// die;
		if($res){
			$res_p=$status=$p->where($map)->save(array('pay_status'=>2));
			alert_json(1,"成功");
		}else{
			alert_json(0,"失败");
		}
	}
	//alipay订单
	public function alipay_order(){
		// dump(session());
        $m=M('Checkbill', C('DB_PREFIX_T'), C('db_config'));

		$param=I('param.','','trim');
		$fee=$param['fee'];
		$mail_no=$param['mail_no'];		
		$pay_no=$param['pay_no'];		
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($fee)){
			$map['fee'] = array('like','%'.$fee.'%');
        }
		if(!empty($mail_no)){
			$map['mail_no'] = array('like','%'.$mail_no.'%');
        }
		if(!empty($pay_no)){
			$map['pay_no'] = array('like','%'.$pay_no.'%');
        }
		$map['agent_id']=session('agent.id');
		//时间查询-begin
		$start_time=$param['start_time'];//开始时间
		$end_time=$param['end_time'];//结束时间
		if(!empty($start_time) && empty($end_time)) {
			$start_time=strtotime($start_time);
			$map['_string']="add_time > '$start_time' ";
        }
        if(empty($start_time) && !empty($end_time)) {
			$end_time=strtotime($end_time)+3600*24;
			$map['_string']="add_time < '$end_time' ";
        }
		if(!empty($start_time) && !empty($end_time)) {
			$start_time=strtotime($start_time);
			$end_time=strtotime($end_time)+3600*24;
			$map['_string']="add_time > '$start_time' and add_time < '$end_time'";
        }
		$map['pay_status']=2;//未支付
		// dump($param);
		//时间查询-end
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// echo $m->getlastsql();
		foreach($list as $k=>$v){
			$list[$k]['add_time']=date('Y-m-d H:i:s',$v['add_time']);
		}
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	public function del_orderdetail(){
		$param = I('param.','','trim');
		$id=$param['id'];
		$m=M('Pushordergoods', C('DB_PREFIX_T'), C('db_config'));
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//批量删除
	public function delAll(){
		$ids=I('ids');
		$map['id']=array('in',$ids);
		$m=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$status=$m->where($map)->delete();
		if($status){
			$return['status']=1;
			$return['info']="删除成功";
			$this->ajaxReturn($return);
		}else{
			$return['status']=0;
			$return['info']="出错";
			$this->ajaxReturn($return);
		}
	}
	public function view_goods(){
		$id=I('id');
		$m=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$m_info=$m->find($id);
		$this->assign('vo',$m_info);
		$this->display();
	}
	//批量删除
	public function delAll_storagegoods(){
		$ids=I('ids');
		$map['id']=array('in',$ids);
		$m=M('Goods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$status=$m->where($map)->delete();
		if($status){
			$return['status']=1;
			$return['info']="删除成功";
			$this->ajaxReturn($return);
		}else{
			$return['status']=0;
			$return['info']="出错";
			$this->ajaxReturn($return);
		}
	}
	//库存列表
	public function storagegoods(){
		$m=M('Goods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$goods_no=I('goods_no');
		$goods_name=I('goods_name');
		$goods_brand=I('goods_brand');
		$excel=I('excel');
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($goods_no)){
			$map['goods_no'] = array('like','%'.$goods_no.'%');
        }
		if(!empty($goods_name)){
			$map['goods_name'] = array('like','%'.$goods_name.'%');
        }
		if(!empty($goods_brand)){
			$map['goods_brand'] = array('like','%'.$goods_brand.'%');
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			// $list[$k]['goods_price']=$this->goods_price($v['goods_price']);
			if($excel){
				$list[$k]['goods_no']="'".$v['goods_no'];
				$list[$k]['net_weight']=sprintf("%.2f",$v['net_weight']);
				$list[$k]['gross_weight']=sprintf("%.2f",$v['gross_weight']);
			}
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		if($excel){
			$xlsName  = "商品列表";
			$xlsCell  = array(
				array('goods_name','商品名称'),
				array('goods_no','商品编码'),
				array('goods_brand','品牌'),
				array('goods_marque','型号'),
				array('goods_spec','规格'),
				array('goods_price','价格'),
				array('storage_amount','库存'),
				array('net_weight','净重(g)'),
				array('gross_weight','毛重(g)')
			);
			$this->exportExcel($xlsName,$xlsCell,$list);
			die;
		}
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
	}
	public function order_detail(){
		$param = I('param.','','trim');

		$g=M('Pushordergoods', C('DB_PREFIX_T'), C('db_config'));
		$p=M('Pushorder', C('DB_PREFIX_T'), C('db_config'));
		
		$id=$param['id'];
		$p_info=$p->find($id);
		$this->assign('detail',$p_info);
		
		$count=$g->where(array('order_id'=>$id))->count();

		$list=$g->where(array('order_id'=>$id))->select();
		
		//$api_url="http://erpvr.jm.com/index.php/Company/Api/product_info";//测试账册商品信息接口
		$api_url="http://www.t-cang.com/erpts/index.php/Company/Api/product_info";
		//$push_data['erp_code']='48';//测试erp_code
		$push_data['erp_code']='37';//erp_code
		
		foreach($list as $k=>$v){
			$push_data['goods_no']=$v['goods_no'];
			$res=push($api_url,$push_data);
			$res=json_decode($res,true);

			if($res['flag']){
				$goods_info=json_decode($res['data'],true);
				// dump($goods_info);
				$list[$k]['goods_name']=$goods_info['name'];
			}
		}

		$this->assign('count',$count);
		$this->assign('list',$list);
		$this->display();
	}
	//商品列表
	public function goods(){
		// dump(session());
        $m=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$bar_code=I('bar_code');
		
		$goods_no=I('goods_no');
		$goods_name=I('goods_name');
		$excel=I('excel');
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		if(!empty($bar_code)){
			$map['bar_code'] = array('like','%'.$bar_code.'%');
        }
        if(!empty($goods_no)){
			$map['goods_no'] = array('like','%'.$goods_no.'%');
        }
		if(!empty($goods_name)){
			$map['goods_name'] = array('like','%'.$goods_name.'%');
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		if($excel){
			$xlsName  = "商品列表";
			$xlsCell  = array(
				array('goods_no','商品编码'),
				array('goods_name','商品名称'),
				array('goods_brand','品牌'),
				array('goods_marque','型号'),
				array('goods_spec','规格'),
				array('price','商品价格'),
				array('receive_amount','数量'),
				array('goods_tax','税率(%)'),
				array('bar_code','条形码'),
				array('source_no','料号'),
				array('net_weight','净重'),
				array('gross_weight','毛重')
			);
			$this->exportExcel($xlsName,$xlsCell,$list);
			die;
		}
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
		//excel导出方法
	public function exportExcel($expTitle,$expCellName,$expTableData){
			$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
			$fileName = $xlsTitle.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
			$cellNum = count($expCellName);
			$dataNum = count($expTableData);
			//vendor("PHPExcel.PHPExcel");
		   
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.IOFactory.php");
		   
		   $date = date("Y_m_d",time());
			$fileName .= "_{$date}.xls";

			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			//$objProps = $objPHPExcel->getProperties();
			
			//$objPHPExcel = new PHPExcel();
			$cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
			
			$objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
		    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle);//设置标题
			$objPHPExcel->getActiveSheet(0)->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //居中
			
			for($i=0;$i<$cellNum;$i++){
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]); 
			} 
			  // Miscellaneous glyphs, UTF-8   
			for($i=0;$i<$dataNum;$i++){
			  for($j=0;$j<$cellNum;$j++){
				$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
			  }             
			}  
			
			header('pragma:public');
			header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
			header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
			$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
			$objWriter->save('php://output');
			exit;
	}

	public function edit_storagegoods(){
		$m=M('Goods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$id=I('id');
		$data['storage_amount']=I('count');
		
		$res=$m->where(array('id'=>$id))->save($data);
		if($res!==false){
			$this->success('修改成功');
		}else{
			$this->error('修改失败');
		}
		
	}
	//添加
	function add_goodss(){
			$trans=M('', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$trans->startTrans();   // 开启事务
			$m=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$g=M('Goods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$param['add_time']=time();
			$bar_code=$param['bar_code'];
			$param['goods_no']=$param['bar_code'];
			$goods_no=$bar_code;//商品编码=条形码
			$nums=$param['receive_amount'];
			$param['storage_amount']=$param['receive_amount'];
			$map['goods_no']=$goods_no;
			if(empty($param['goods_name'])){
				alert_json(0,'商品名称不能为空');
			}
			if(empty($param['bar_code'])){
				alert_json(0,'条形码不能为空');
			}
			if(empty($param['storage_amount'])){
				alert_json(0,'库存不能为空');
			}
			if (!empty($_FILES)){
				$config = array(
					'maxSize'    =>    3145728,    
					'savePath'   =>    '/Import/',    
					'saveName'   =>    array('uniqid',''),    
					'exts'       =>    array('jpg','jpeg','png','gif'),    
					'autoSub'    =>    true,    
					'subName'    =>    array('date','Ymd'),);
				$upload = new \Think\Upload($config);// 实例化上传类
				$info   =   $upload->upload();    
				if(!$info) {// 上传错误提示错误信息        
					$this->error($upload->getError());    
				}
				$extension=$info['goods_pic']['ext'];
				$file_name='./Uploads'.$info['goods_pic']['savepath'].$info['goods_pic']['savename'];
				
				// textgd($file_name);//加水印
				$param['goods_pic']=$file_name;
			}
			// dump($param);
			$res=$m->add($param);
			if($res){
				$g_info=$g->where($map)->find();//更新库存
				if(empty($g_info)){//没商品信息则新增商品信息
					$data_goods['bar_code']=$param['bar_code'];
					$data_goods['goods_no']=$param['goods_no'];
					$data_goods['goods_name']=$param['goods_name'];
					$data_goods['goods_brand']=$param['goods_brand'];
					$data_goods['goods_marque']=$param['goods_marque'];
					$data_goods['goods_spec']=$param['goods_spec'];
					$data_goods['goods_pic']=$param['goods_pic'];
					$data_goods['goods_price']=$param['price'];
					$data_goods['storage_amount']=$param['receive_amount'];//库存数量
					$data_goods['amount']=$param['receive_amount'];//总数量
					$data_goods['net_weight']=$param['net_weight'];
					$data_goods['gross_weight']=$param['gross_weight'];
					$res_goods=$g->add($data_goods);
					if(!$res_goods){
						$trans->rollback();
						alert_json(0,'添加库存信息失败');
					}
				}else{//有商品信息则更新库存
					$this->edit_storagegoodsnums($goods_no,$nums);
				}
				$trans->commit();
				alert_json(1,'添加成功');
			}else{
				$trans->rollback();
				alert_json(0,'添加失败');
			}
	}
	//更新
	function add_goods(){
		if(IS_POST){
			$m=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$param['update_time'] = time();
			$id=$param['id'];
			$param['goods_no']=$param['bar_code'];
			if(empty($param['goods_name'])){
				alert_json(0,'商品名称不能为空');
			}
			if(empty($param['bar_code'])){
				alert_json(0,'条形码不能为空');
			}
			if(empty($param['receive_amount'])){
				alert_json(0,'库存不能为空');
			}
			if (!empty($_FILES)){
				$config = array(
					'maxSize'    =>    3145728,    
					'savePath'   =>    '/Import/',    
					'saveName'   =>    array('uniqid',''),    
					'exts'       =>    array('jpg','jpeg','png','gif'),    
					'autoSub'    =>    true,    
					'subName'    =>    array('date','Ymd'),);
				$upload = new \Think\Upload($config);// 实例化上传类
				$info   =   $upload->upload();    
				if(!$info) {// 上传错误提示错误信息        
					$this->error($upload->getError());    
				}
				$extension=$info['goods_pic']['ext'];
				$file_name='./Uploads'.$info['goods_pic']['savepath'].$info['goods_pic']['savename'];
				$param['goods_pic']=$file_name;
			}
			$res=$m->where(array('id'=>$id))->save($param);
			if($res!==false){
				alert_json(1,'更新成功');
			}
		}else{
			$this->display();
		}
	}
	//编辑
	function edit_goods(){
		$id=trim(I('id'));
		$m=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_goods(){
		$id=trim(I('id'));
		$m=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//账册列表
	// public function get_booklist($warehouse_code){
		// $url=C('BASE_CONFIG.get_booklist');
		// $data['warehouse_code']=$warehouse_code;
		// $res=push($url,$data);
		// $res=json_decode($res,true);
		// return $res;
	// }

	//货主列表及配置调用
	public function is_owner($username,$warehouse_code){
		$u=M('User');
		$erp_code=session('agent.id');
		$wheres['_string']="FIND_IN_SET($erp_code,erp_code)";
		$u_list=$u->where($wheres)->select();
		// $username=$param['username'];
		if(empty($username)){
			$username=$u_list[0]['username'];
		}
		$db_name=C('DB_PREFIX_T').$username;
		C('db_config.DB_NAME',$db_name);//当前
		$base_config=M('Config',C('DB_PREFIX_T'), C('db_config'));
		$list=$base_config->getField('en_name,value');
		C('base_config',$list);
		$uw=M('UserWarehouse');
		$w=M('Warehouse');
		// dump($u_list);
		foreach($u_list as $k=>$v){
			$warehouse_group_id=$uw->where(array('user_id'=>$v['id']))->getField('warehouse_group_id');
			// dump($warehouse_group_id);
			$w_list=$w->field('warehouse_code')->where(array('id'=>array('in',$warehouse_group_id)))->select();
			$res=deep_in_array($warehouse_code,$w_list);
			if(!$res){
				unset($u_list[$k]);
			}
		}
		$this->assign('u_list',$u_list);
		$this->assign('username',$username);
	}
	//订单列表
	public function order(){
		$param = I('param.','','trim');
		
        $m=M('Pushorder', C('DB_PREFIX_T'), C('db_config'));
		$g=M('Pushordergoods', C('DB_PREFIX_T'), C('db_config'));

		$order_no=$param['order_no'];
		$logistics_no=$param['logistics_no'];
		$user_name=$param['user_name'];
		$user_idcard=$param['user_idcard'];
		$user_phone=$param['user_phone'];
		$user_address=$param['user_address'];
		$start_time=$param['start_time'];
		$end_time=$param['end_time'];
		$flag=$param['flag'];
		
		$warehouse_code=$param['warehouse_code'];//当前仓库编码
		
		!empty($start_time)?$start_time = strtotime($start_time):$start_time=0;
		!empty($end_time)?$end_time = strtotime($end_time):$end_time =time();
		$map['_string'] = "add_time>=$start_time AND add_time<=$end_time";
		if(!empty($flag)){
			$map['flag']=$flag;
		}
		if(!empty($warehouse_code)){
			$map['warehouse_code']=$warehouse_code;
		}
		$map['agent_id']=session('agent.id');
		$map['status']=array('neq',-1);
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		
        if(!empty($order_no)){
			$map['old_order_no'] = array('like','%'.$order_no.'%');
        }
		if(!empty($logistics_no)){
			$map['logistics_no'] = array('like','%'.$logistics_no.'%');
        }
		if(!empty($user_idcard)){
			$map['user_idcard'] = array('like','%'.$user_idcard.'%');
        }
		if(!empty($user_phone)){
			$map['user_phone'] = array('like','%'.$user_phone.'%');
        }
		if(!empty($user_address)){
			$map['user_address'] = array('like','%'.$user_address.'%');
        }
		if(!empty($user_name)){
			$map['user_name'] = array('like','%'.$user_name.'%');
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// echo $m->getlastsql();
		// dump($list);
		
		//导出Excel
		$excel=I('excel');
		
		
		foreach($list as $k=>$v){
			if(!empty($excel)){ //导出Excel
				$list[$k]['old_order_no']=' '.$v['old_order_no'];
				$list[$k]['user_phone']=' '.$v['user_phone'];
				$g_list=$g->where(array('order_id'=>$v['id']))->select();
				foreach($g_list as $ks=>$vs){
					if($ks){
						$list[$k]['bar_code'].='&'.substr($vs['goods_no'],11);
						$list[$k]['goods_nums'].='&'.$vs['goods_nums'];
						$list[$k]['goods_price'].='&'.$vs['goods_price'];
					}else{
						$list[$k]['bar_code']=' '.substr($vs['goods_no'],11);
						$list[$k]['goods_nums']=$vs['goods_nums'];
						$list[$k]['goods_price']=$vs['goods_price'];
					}
				}
			}else{
				$g_list=$g->where(array('order_id'=>$v['id']))->select();
				$total_price=0;
				foreach($g_list as $ks=>$vs){
					$total_price+=$vs['goods_nums']*$vs['sell_price'];
					$list[$k]['total_price']=$total_price;
				}
			}
		}

		if(!empty($excel)){ //导出Excel
			
			$xlsName  = "订单管理";
			$xlsCell  = array(
				array('id','ID'),
				array('old_order_no','订单编号'),
				array('logistics_no','运单号'),
				array('user_name','收件人姓名'),
				array('user_phone','收件人电话'),
				array('province','省'),
				array('city','市'),
				array('area','区'),
				array('user_address','收件人地址'),
				array('bar_code','商品条形码'),
				array('goods_nums','商品数量'),
				array('goods_price','单价')
			);
			$this->exportExcel($xlsName,$xlsCell,$list);
			die;
		}
		

		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//添加
	function add_orders(){
			$param = I('param.','','trim');

			$m=M('Pushorder', C('DB_PREFIX_T'), C('db_config'));
			$p=M('Printorder', C('DB_PREFIX_T'), C('db_config'));
			$this->verification($param);
			$param['add_time']=time();

			$map['order_no']=I('order_no');
			$duplicate=$m->where($map)->find();
			if($duplicate){
				$return['message']='订单编码已经存在';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			$print_data['sendMan']=$param['sendMan'];
			$print_data['sendManPhone']=$param['sendManPhone'];
			$print_data['sendProvince']=$param['sendProvince'];
			$print_data['sendCity']=$param['sendCity'];
			$print_data['sendCounty']=$param['sendCounty'];
			$print_data['sendManAddress']=$param['sendManAddress'];
			$print_data['receiveMan']=$param['user_name'];
			$print_data['receiveManPhone']=$param['user_phone'];
			$print_data['receiveManAddress']=$param['user_address'];
			$print_data['receiveProvince']=$param['province'];
			$print_data['receiveCity']=$param['city'];
			$print_data['receiveCounty']=$param['area'];
			
			unset($param['username']);
			$res=$m->add($param);
			// dump($param['warehouse_code']);
			// die;
			if($res){
				$status=$p->add($print_data);//添加面单信息
				if(!$status){
					$m->where(array('id'=>$res))->delete();//回滚
					$return['message']='面单信息添加失败';
					$return['status']=false;
					$this->ajaxReturn($return);
				}
				$return['message']='添加成功';
				$return['status']=true;
				$this->ajaxReturn($return);
			}
	}
	//验证
	public function verification($param){
			if(empty($param['order_no'])){
				$return['message']='订单编号不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			// if(empty($param['logistics_no'])){
				// $return['message']='电子面单号不能为空';
				// $return['status']=false;
				// $this->ajaxReturn($return);
			// }
			if(!mobile($param['user_phone'])){
				$return['message']='收件人电话(手机号)错误';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['user_name'])){
				$return['message']='收件人姓名不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			// if(empty($param['user_idcard'])){
				// $return['message']='身份证号不能为空';
				// $return['status']=false;
				// $this->ajaxReturn($return);
			// }
			if(empty($param['user_phone'])){
				$return['message']='收件人电话不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['province'])){
				$return['message']='到件省不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['city'])){
				$return['message']='到件市不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['area'])){
				$return['message']='到件区/县不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['user_address'])){
				$return['message']='详细地址不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			if(empty($param['total_price'])){
				$return['message']='总价不能为空';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			// if(strlen($param['user_idcard'])!=15 and strlen($param['user_idcard'])!=18){
				// $return['message']='身份证号位数必须为15位或18位';
				// $return['status']=false;
				// $this->ajaxReturn($return);
			// }
	}
	public function city_list(){
		$code=I('code');
		$city_list=city_list($code);
		alert_json(1,$city_list);
	}
	public function area_list(){
		// dump(I());
		// die;
		$code=I('code');
		$province_name=I('province_name');
		$area_list=area_list($province_name,$code);
		alert_json(1,$area_list);
	}
	//推送备注信息
	public function pushwms($id,$warehouse_code){
		$m=M('Pushorder',C('DB_PREFIX_T'),C('db_config'));
		$data=$m->field('logistics_no,erp_mark_mailno')->where(array('id'=>$id))->find();
		$data['warehouse_code']=$warehouse_code;
		$url=C('BASE_CONFIG.pushwms_mark');
		$res=push($url,$data);

		$res=json_decode($res,true);
		if($res['status']){
			alert_json(1,'成功!');
		}else{
			alert_json(0,$res['message']);
		}
	}
	
	//事务测试
	public function test(){
			C('db_config.db_name','erp_18098765408');
			$trans_08=M('','tz_',C('db_config'));
			// $trans_08->startTrans();   // 开启事务
			$m=M('Test','tz_',C('db_config'));
			C('db_config.db_name','erp_18012345678');
			$trans_78=M('','tz_',C('db_config'));
			// $trans_78->startTrans();   // 开启事务
			$n=M('Test','tz_',C('db_config'));
			$data['name']='123';
			$res_m=$m->add($data);
			$res_n=$n->add($data);
			// $trans_res1=$trans_08->rollback();
			// $trans_res2=$trans_78->rollback();
			dump($res_m);
			dump($res_n);
			// dump($res_n);
	}
	//订单总费用
	function total_fee(){
		$param=I('param.','','trim');
		$ids=$param['ids'];
		$g=M('Pushordergoods', C('DB_PREFIX_T'), C('db_config'));
		$map['order_id']=array('in',$ids);
		$g_list=$g->field('sell_price,goods_nums')->where($map)->select();
		$total_fee=0;
		foreach($g_list as $k=>$v){
			$total_fee+=$v['sell_price']*$v['goods_nums'];
		}
		if(empty($total_fee)){
			alert_json(0,0);
		}else{
			alert_json(1,$total_fee);
		}
	}
	//更新
	function add_order(){
		$province_list=province_list();
		$this->assign('province_list',$province_list);
		
		//流水号
		$outbound=A('Outbound');
		$serialNumber=$outbound->serialNumber('Pushorder');
		$serialNumber='P'.date("Ymd",time()).$serialNumber;
		$this->assign("serialNumber",$serialNumber);
		//流水号
		
		if(IS_POST){
			$param = I('param.','','trim');
			$id=$param['id'];
			$m=M('Pushorder', C('DB_PREFIX_T'), C('db_config'));
			$p=M('Printorder', C('DB_PREFIX_T'), C('db_config'));
			$m_info=$m->where(array('id'=>$id))->find();
			$this->verification($param);
			
			$print_data['sendMan']=$param['sendMan'];
			$print_data['sendManPhone']=$param['sendManPhone'];
			$print_data['sendProvince']=$param['sendProvince'];
			$print_data['sendCity']=$param['sendCity'];
			$print_data['sendCounty']=$param['sendCounty'];
			$print_data['sendManAddress']=$param['sendManAddress'];
			$print_data['receiveMan']=$param['user_name'];
			$print_data['receiveManPhone']=$param['user_phone'];
			$print_data['receiveManAddress']=$param['user_address'];
			$print_data['receiveProvince']=$param['province'];
			$print_data['receiveCity']=$param['city'];
			$print_data['receiveCounty']=$param['area'];
			
			$param['update_time'] = time();
			
			$order_no=$param['order_no'];
			unset($param['order_no']);
			$res=$m->where(array('id'=>$id))->save($param);
			if($res!==false){
				$warehouse_code=$param['warehouse_code'];
				if($m_info['push_status']){
					$this->pushwms($id,$warehouse_code);
				}
				$status=$p->where(array('order_no'=>$order_no))->save($print_data);
				if($status===false){
					$return['message']='更新面单信息失败';
					$return['status']=false;
					$this->ajaxReturn($return);
				}
				$return['message']='更新成功';
				$return['status']=true;
				$this->ajaxReturn($return);
			}
		}else{
			$this->display();
		}
	}

	
	//购买人
	public function edit_buyer(){
		$param = I('param.','','trim');
		$id=$param['id'];
		$m=M('Pushorder',C('DB_PREFIX_T'),C('db_config'));
		$info=$m->field('buyer_idcard,buyer_name')->where(array('id'=>$id))->find();
		
		$this->assign('detail',$info);
		$this->display();
	}
	//编辑购买人信息
	public function confirm_edit_buyer(){
		$param = I('param.','','trim');

		$id=$param['id'];
		$data['buyer_name']=$param['buyer_name'];
		$data['buyer_idcard']=$param['buyer_idcard'];
		$m=M('Pushorder',C('DB_PREFIX_T'),C('db_config'));
		$res=$m->where(array('id'=>$id))->save($data);
		
		if($res!==false){
			alert_json(1,'成功');
		}else{
			alert_json(0,'失败');
		}
	}
	//再次推送
	public function pushoms_two(){
		$param = I('param.','','trim');
		
		$m=M('Pushorder',C('DB_PREFIX_T'),C('db_config'));
		$p=M('Product',C('DB_PREFIX_T'),C('db_config'));
		$g=M('Pushordergoods',C('DB_PREFIX_T'),C('db_config'));
		$u=M('User');

		// dump($param);
		$ids=$param['ids'];
		$url=C('base_config.pushoms_two');
		// dump($url);
		// die;
		$map['id']=array('in',$ids);
		$list=$m->field('id,old_order_no order_no,buyer_name,buyer_idcard')->where($map)->select();
		// echo $m->getlastsql();
		
		$data['data']=$list;
		
		

			$data['customer']['user_code']=session('agent.user_code');
			$data['customer']['user_key']=session('agent.user_key');
		

		$data['unusual_order']=1;
		//dump($list);
		//die;
		$res=send_curl($url,$data);
		$res=json_decode($res,true);
		
		if($res['flag']){
			$return['message']=$res['tip'];
			$return['status']=1;
			$this->ajaxReturn($return);
		}else{
			//更新状态-end
			$return['message']=$res['tip'];
			$return['status']=0;
			$this->ajaxReturn($return);
		}
	}
	//订单推送(一般正常)
	public function pushoms_normal_normal(){
		$param = I('param.','','trim');

		
		$m=M('Pushorder',C('DB_PREFIX_T'),C('db_config'));
		$p=M('Product',C('DB_PREFIX_T'),C('db_config'));
		$g=M('Pushordergoods',C('DB_PREFIX_T'),C('db_config'));
		$u=M('User');

		$ids=$param['ids'];
		$url=C('base_config.pushoms_normal');
		$map['id']=array('in',$ids);
		$erp_code=$u->where(array('username'=>$param['username']))->getField('id');//货主erp账号id(erp_code)
		
		$list=$m->field("id,old_order_no order_no,user_name,user_phone,total_price order_amount,province user_province,city user_city,area user_area,user_address,erp_mark_mailno remark,$erp_code erp_code,transport_cate,'ERP' platform_name")->where($map)->select();
		// echo $m->getlastsql();
		foreach($list as $k=>$v){
			$list[$k]['order_detail']=$g->alias('g')->field('g.goods_no goods_sn,p.name goods_name,g.goods_price,g.goods_nums,g.goods_batch_no')->join('__PRODUCT__ p on g.goods_no=p.goods_no','left')->where(array('order_id'=>$v['id']))->select();
			foreach($list[$k]['order_detail'] as $ks=>$vs){
				// $list[$k]['order_detail'][$ks]['warehouse_code']=C('base_config.warehouse_code_normal');
				$list[$k]['order_detail'][$ks]['warehouse_code']=I('warehouse_code');
				if(empty($vs['goods_price']) or $vs['goods_price']=='0.00'){
					$list[$k]['order_detail'][$ks]['goods_price']=$p->where(array('goods_no'=>$vs['goods_sn']))->getField('price');
				}
			}
		}
		$data['data']=$list;

			$data['customer']['user_code']=session('agent.user_code');
			$data['customer']['user_key']=session('agent.user_key');
		
		
		$data['trade_model']=0;//非跨境
		file_put_contents('./log/pushoms_normal_normal'.date('Y-m-d').'.log',date('Y-m-d H:i:s').var_export($data,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$res=send_curl($url,$data);
		$res=json_decode($res,true);
		$setstatus['push_status']=1;
		if($res['flag']){
			$m->where($map)->save($setstatus);
			//更新运单号
			foreach($res['data']['detail'] as $ks=>$vs){
				$data_order['logistics_no']=$vs['logistics_no'];
				$m->where(array('old_order_no'=>$vs['order_no']))->save($data_order);
			}
			$return['message']=$res['tip'];
			$return['status']=true;
			$this->ajaxReturn($return);
		}else{
			//更新状态-begin
			foreach($res['data']['detail'] as $ks=>$vs){
				// $map_order['old_order_no'][]=$vs['order_no'];
				if(!empty($vs['logistics_no'])){
					$data_order['logistics_no']=$vs['logistics_no'];
					$m->where(array('old_order_no'=>$vs['order_no']))->save($data_order);
				}else{
					$old_order_no[]=$vs['order_no'];
				}
			}
			if(!empty($old_order_no)){
				$map_order['old_order_no']=array('in',$old_order_no);
				$list_m=$m->field('id')->where($map_order)->select();
				foreach($list_m as $k=>$v){
					$ids_m[]=$v['id'];
				}
				$idss=array_diff_assoc($ids,$ids_m);
				$map_m['id']=array('in',$idss);
				$data_order['push_status']=0;
				$m->where($map_m)->save($data_order);
			}
			//更新状态-end
			$return['message']=$res['tip'];
			$return['status']=false;
			$this->ajaxReturn($return);
		}
	}
	//订单推送(跨境正常)
	public function pushoms_normal(){
		$param = I('param.','','trim');

		
		$m=M('Pushorder',C('DB_PREFIX_T'),C('db_config'));
		$p=M('Product',C('DB_PREFIX_T'),C('db_config'));
		$g=M('Pushordergoods',C('DB_PREFIX_T'),C('db_config'));
		$u=M('User');
		
		$ids=$param['ids'];
		$url=C('base_config.pushoms_aozhou');
		$map['id']=array('in',$ids);
		$erp_code=$u->where(array('username'=>$param['username']))->getField('id');//货主erp账号id(erp_code)
		$list=$m->field("id,old_order_no order_no,user_name,user_phone,user_phone buyer_phone,buyer_idcard,buyer_name,province user_province,city user_city,erp_mark_mailno remark,$erp_code erp_code,area user_area,user_address")->where($map)->select();
		// echo $m->getlastsql();
		foreach($list as $k=>$v){
			$list[$k]['order_detail']=$g->alias('g')->field('g.goods_no goods_sn,p.name goods_name,g.goods_nums,g.goods_price,g.goods_batch_no')->join('__PRODUCT__ p on g.goods_no=p.goods_no','left')->where(array('order_id'=>$v['id']))->select();
			$total_price=0;//订单总价格
			$order_tax=0;//订单总税金
			foreach($list[$k]['order_detail'] as $ks=>$vs){
				$goods_info=$p->where(array('goods_no'=>$vs['goods_sn']))->find();
				$list[$k]['order_detail'][$ks]['source_no']=$goods_info['pn'];
				if(empty($vs['goods_price']) or $vs['goods_price']=='0.00'){
					$list[$k]['order_detail'][$ks]['goods_price']=sprintf("%.2f",$goods_info['price']);
				}else{
					$goods_info['price']=$vs['goods_price'];
				}
				$list[$k]['order_detail'][$ks]['net_weight']=$goods_info['net_weight'];
				$list[$k]['order_detail'][$ks]['gross_weight']=$goods_info['gross_weight'];
				$goods_price=sprintf("%.2f",$goods_info['price']*$vs['goods_nums']);//商品总价格=单价*数量
				$goods_tax=sprintf("%.2f",$goods_price*$goods_info['goods_tax']);//税金=商品总价格*税率
				$total_price+=$goods_price+$goods_tax;//总价格
				$order_tax+=$goods_tax;//订单总税金
			}
			if(empty($v['buyer_name'])){//购买人为空
				$list[$k]['buyer_name']=$list[$k]['user_name'];
			}
			$list[$k]['order_amount']=$total_price;
			$list[$k]['order_tax']=$order_tax;
		}
		$data['data']=$list;

			$data['customer']['user_code']=session('agent.user_code');
			$data['customer']['user_key']=session('agent.user_key');
		
		file_put_contents('./log/pushoms_normal'.date('Y-m-d').'.log',date('Y-m-d H:i:s').var_export($data,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$res=send_curl($url,$data);
		$res=json_decode($res,true);

		$setstatus['push_status']=1;
		if($res['flag']){
			$m->where($map)->save($setstatus);
			//更新运单号
			foreach($res['data']['detail'] as $ks=>$vs){
				$data_order['logistics_no']=$vs['logistics_no'];
				$m->where(array('old_order_no'=>$vs['order_no']))->save($data_order);
			}
			$return['message']=$res['tip'];
			$return['status']=true;
			$this->ajaxReturn($return);
		}else{
			//更新状态-begin
			foreach($res['data']['detail'] as $ks=>$vs){
				// $map_order['old_order_no'][]=$vs['order_no'];
				if(!empty($vs['logistics_no'])){
					$data_order['logistics_no']=$vs['logistics_no'];
					$m->where(array('old_order_no'=>$vs['order_no']))->save($data_order);
				}else{
					$old_order_no[]=$vs['order_no'];
				}
			}
			if(!empty($old_order_no)){
				$map_order['old_order_no']=array('in',$old_order_no);
				$list_m=$m->field('id')->where($map_order)->select();
				foreach($list_m as $k=>$v){
					$ids_m[]=$v['id'];
				}
				$idss=array_diff_assoc($ids,$ids_m);
				$map_m['id']=array('in',$idss);
				$data_order['push_status']=0;
				$m->where($map_m)->save($data_order);
			}
			//更新状态-end
			$return['message']=$res['tip'];
			$return['status']=false;
			$this->ajaxReturn($return);
		}
	}
	//订单推送(澳洲)
	public function pushoms_aozhou(){
		$param = I('param.','','trim');

		$m=M('Pushorder',C('DB_PREFIX_T'),C('db_config'));
		$p=M('Product',C('DB_PREFIX_T'),C('db_config'));
		$g=M('Pushordergoods',C('DB_PREFIX_T'),C('db_config'));
		$u=M('User');
		
		$ids=$param['ids'];
		
		$url=C('base_config.pushoms_aozhou');
		// dump($url);
		// die;
		$map['id']=array('in',$ids);
		$erp_code=$u->where(array('username'=>$param['username']))->getField('id');//货主erp账号id(erp_code)
		$list=$m->field("id,old_order_no order_no,user_name,user_phone,user_phone buyer_phone,user_idcard buyer_idcard,buyer_name,province user_province,erp_mark_mailno remark,$erp_code erp_code,city user_city,area user_area,user_address")->where($map)->select();
		// echo $m->getlastsql();
		foreach($list as $k=>$v){
			$list[$k]['order_detail']=$g->field('goods_no goods_sn,goods_name,goods_nums,goods_price,goods_batch_no')->where(array('order_id'=>$v['id']))->select();
			$total_price=0;//订单总价格
			$order_tax=0;//订单总税金
			foreach($list[$k]['order_detail'] as $ks=>$vs){
				$goods_info=$p->where(array('goods_no'=>$vs['goods_sn']))->find();
				$list[$k]['order_detail'][$ks]['source_no']=$goods_info['pn'];
				if(empty($vs['goods_price']) or $vs['goods_price']=='0.00'){
					$list[$k]['order_detail'][$ks]['goods_price']=sprintf("%.2f",$goods_info['price']);
				}else{
					$goods_info['price']=$vs['goods_price'];
				}
				$list[$k]['order_detail'][$ks]['goods_price']=sprintf("%.2f",$goods_info['price']);
				$list[$k]['order_detail'][$ks]['net_weight']=$goods_info['net_weight'];
				$list[$k]['order_detail'][$ks]['gross_weight']=$goods_info['gross_weight'];
				$goods_price=sprintf("%.2f",$goods_info['price']*$vs['goods_nums']);//商品总价格=单价*数量
				$goods_tax=sprintf("%.2f",$goods_price*$goods_info['goods_tax']);//税金=商品总价格*税率
				$total_price+=$goods_price+$goods_tax;//总价格
				$order_tax+=$goods_tax;//订单总税金
			}
			if(empty($v['buyer_name'])){//购买人为空
				$list[$k]['buyer_name']=$list[$k]['user_name'];
			}
			$list[$k]['order_amount']=$total_price;
			$list[$k]['order_tax']=$order_tax;
		}
		$data['data']=$list;

		
			$data['customer']['user_code']=session('agent.user_code');
			$data['customer']['user_key']=session('agent.user_key');
		
		file_put_contents('./log/pushoms_aozhou'.date('Y-m-d').'.log',date('Y-m-d H:i:s').var_export($data,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$res=send_curl($url,$data);
		$res=json_decode($res,true);

		$setstatus['push_status']=1;
		if($res['flag']){
			$m->where($map)->save($setstatus);
			//更新运单号
			foreach($res['data']['detail'] as $ks=>$vs){
				$data_order['logistics_no']=$vs['logistics_no'];
				$m->where(array('old_order_no'=>$vs['order_no']))->save($data_order);
			}
			$return['message']=$res['tip'];
			$return['status']=true;
			$this->ajaxReturn($return);
		}else{
			//更新状态-begin
			foreach($res['data']['detail'] as $ks=>$vs){
				// $map_order['old_order_no'][]=$vs['order_no'];
				if(!empty($vs['logistics_no'])){
					$data_order['logistics_no']=$vs['logistics_no'];
					$m->where(array('old_order_no'=>$vs['order_no']))->save($data_order);
				}else{
					$old_order_no[]=$vs['order_no'];
				}
			}
			if(!empty($old_order_no)){
				$map_order['old_order_no']=array('in',$old_order_no);
				$list_m=$m->field('id')->where($map_order)->select();
				foreach($list_m as $k=>$v){
					$ids_m[]=$v['id'];
				}
				$idss=array_diff_assoc($ids,$ids_m);
				$map_m['id']=array('in',$idss);
				$data_order['push_status']=0;
				$m->where($map_m)->save($data_order);
			}
			//更新状态-end
			$return['message']=$res['tip'];
			$return['status']=false;
			$this->ajaxReturn($return);
		}
	}
	
	//推送oms
	public function pushoms(){
		$param = I('param.','','trim');

		$m=M('Pushorder', C('DB_PREFIX_T'), C('db_config'));
		$g=M('Pushordergoods', C('DB_PREFIX_T'), C('db_config'));

		$ids=$param['ids'];
		$url=C('base_config.pushoms');
		$map['id']=array('in',$ids);
		$list=$m->field('id,old_order_no order_no,user_name,user_phone,user_idcard buyer_idcard,user_name buyer_name,province user_province,city user_city,area user_area,user_address,total_price order_amount')->where($map)->select();
		foreach($list as $k=>$v){
			$list[$k]['order_detail']=$g->field('goods_no goods_sn,goods_name,goods_nums,goods_price')->where(array('order_id'=>$v['id']))->select();
		}
		$data['data']=$list;
		$u=M('User');

			$data['customer']['user_code']=session('agent.user_code');
			$data['customer']['user_key']=session('agent.user_key');
		
		// dump($data);
		$res=send_curl($url,$data);
		$res=json_decode($res,true);
		if($res['flag']){
			//更新运单号
			foreach($res['data']['detail'] as $ks=>$vs){
				$data_order['logistics_no']=$vs['logistics_no'];
				$m->where(array('order_no'=>$vs['order_no']))->save($data_order);
			}
			$return['message']=$res['tip'];
			$return['status']=true;
			$this->ajaxReturn($return);
		}else{
			$return['message']=$res['tip'];
			$return['status']=false;
			$this->ajaxReturn($return);
		}
	}
	//编辑
	function edit_order(){
		$param = I('param.','','trim');

		
		$id=$param['id'];
		$m=M('Pushorder', C('DB_PREFIX_T'), C('db_config'));
		$detail=$m->where(array('id'=>$id))->find();
		
		$province_list=province_list();
		$this->assign('province_list',$province_list);
		$city_list=city_list($detail['province']);
		$this->assign('city_list',$city_list);
		$area_list=area_list($detail['province'],$detail['city']);
		$this->assign('area_list',$area_list);
		
		$send_city_list=city_list($detail['sendprovince']);
		$this->assign('send_city_list',$send_city_list);
		$send_area_list=area_list($detail['sendprovince'],$detail['sendcity']);
		$this->assign('send_area_list',$send_area_list);
		
		$this->assign('detail',$detail);
		$this->display();
	}
	
	//删除
	public function del_order(){
		$param = I('param.','','trim');
		$id=$param['id'];
		$m=M('Pushorder', C('DB_PREFIX_T'), C('db_config'));
		$map['id']=$id;
		
		$url=C('base_config.order_del');
		$m_info=$m->where($map)->find();
		$pay_status=$m_info['pay_status'];
		
		if(empty($pay_status)){//未推送
			//删除订单-自动回调库存-begin
			$g=M('Pushordergoods', C('DB_PREFIX_T'), C('db_config'));
			$g_list=$g->field('goods_batch_no')->where(array('order_id'=>$id))->select();
			if(empty($g_list)){
				echo "没有查到冻结库存数据";
				die;
			}
			// $api_url="http://es.jm.com/index.php?s=/Pushreceiveapi/release_storage";//测试释放库存接口
			$api_url="http://www.t-cang.com/wmstest/index.php?s=/Pushreceiveapi/release_storage";//释放库存接口
			$push_data=array();
			$push_data['g_list']=$g_list;
			$push_data['warehouse_code']=$param['warehouse_code'];
			$push_data['agent_id']=session('agent.id');
			
			$res_g=push($api_url,$push_data);
			$res_g=json_decode($res_g,true);
			if(!$res_g['status']){
				echo "回调库存失败，请联系管理员";
				die;
			}
			//删除订单-自动回调库存-end
		
			$data['status'] = -1;
			$status=$m->where($map)->save($data);
			if($status!==false){
				echo "y";
			}else{
				echo "删除成功";
			}
		}else{//已推送 走删单接口
			echo "支付订单不能删除";
			exit;
		
			$send_data['order_no']=$m_info['old_order_no'];
			$send_data['warehouse_code']=$m_info['warehouse_code'];
			$res=send_curl($url,$send_data);
			$res=json_decode($res,true);
			if($res['flag']==1){	//1:成功0：失败4等待
				$data['status'] = -1;
				$status=$m->where($map)->save($data);
				if($status!==false){
					echo "y";
				}else{
					echo "删除成功";
				}
			}
			if($res['flag']==0){	//1:成功0：失败4等待
				echo "删除失败";
			}
			if($res['flag']==4){	//1:成功0：失败4等待
				$data['status'] = 4;
				$status=$m->where($map)->save($data);
				if($status!==false){
					echo "y";
				}else{
					echo "等待回执状态";
				}
			}
		}
	}
	//更新总库存(奶粉周流程)
	//$goods_no 商品编码
	//$nums 数量
	//$add 增加 false减少
	public function edit_storagegoodsnums($goods_no,$nums,$add=true,$inbound=false){
		$g=M('Goods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['goods_no']=$goods_no;
		$info=$g->where($map)->find();
		// dump($info);
		if(empty($info)){
			alert_json(0,'库存中没有查到对应商品');
		}
		if($add){
			if($inbound){//入库则总数量叠加
				$data['amount']=array('exp','amount+'.$nums);//总数量
			}
			$data['storage_amount']=array('exp','storage_amount+'.$nums);//剩余库存
		}else{
			if($info['storage_amount']-$nums<0){
				alert_json(0,'库存不足');
			}
			$data['storage_amount']=array('exp','storage_amount-'.$nums);//剩余库存
		}
		$res=$g->where($map)->save($data);
		if($res!==false){
			return 1;
		}else{
			// $trans->rollback();
			alert_json(0,'扣减库存出错');
		}
	}
}